//
//  LoginViewController.m
//  Travel assistant
//
//  Created by 王京 on 16/3/10.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "ForgetViewController.h"
#import <UMSocial.h>
#import "MyViewController.h"
#import <UIImageView+WebCache.h>
#import "DataHelper.h"
#import "UserOfMine.h"
@interface LoginViewController ()<UMSocialUIDelegate>

@property(nonatomic,strong) UITextField *phoneNumberText;

@property (nonatomic,strong) UIImageView *titleImageV;

@property (nonatomic,strong) UITextField *passWordText;

@property (nonatomic,strong) UIButton *forgetBtn;

@property (nonatomic,strong) UIButton *registBtn;

@property (nonatomic,strong) UIButton *QQBtn;

@property (nonatomic,strong) UIButton *sinaBtn;

@property (nonatomic,strong) UserOfMine *mine;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self add];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"登陆" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBtnAction)];
    
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    [self.registBtn addTarget:self action:@selector(registBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.forgetBtn addTarget:self action:@selector(forgetBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [self.QQBtn addTarget:self action:@selector(QQBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [self.sinaBtn addTarget:self action:@selector(sinaBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
//    [self.navigationItem setHidesBackButton:YES];
    
    
    // Do any additional setup after loading the view.
}


- (void)rightBtnAction{
    
    if (self.phoneNumberText.text.length && self.passWordText.text.length) {
        [AVUser logInWithUsernameInBackground:self.phoneNumberText.text password:self.passWordText.text block:^(AVUser *user, NSError *error) {
            if (user) {
                //当前用户
                AVUser *user = [AVUser currentUser];
                MyViewController *my = [MyViewController new];
                HELP.loginToView = YES;
                HELP.userName = user.username;
                HELP.placeHoderImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"9.jpg"]];
                
                AVQuery *query = [AVQuery queryWithClassName:@"UserInfo"];
                [query getObjectInBackgroundWithId:user.username block:^(AVObject *object, NSError *error) {
                    if (!error) {
                        
                        
                    }else{
                        NSLog(@"%@",error);
                    }
                }];
                
                
                
                
                [self.navigationController pushViewController:my animated:YES];
                
            }else{
                if (error.code == 211) {//用户不存在
                    UIAlertController *alertContrller = [UIAlertController alertControllerWithTitle:@"用户不存在" message:@"请输入正确的用户名" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        self.phoneNumberText.text = nil;
                        self.passWordText.text = nil;
                    }];
                    [alertContrller addAction:sureAlertAction];
                    [self presentViewController:alertContrller animated:YES completion:nil];
                }
                if (error.code == 210) {//添加用户密码错误
                    UIAlertController *alertContrller = [UIAlertController alertControllerWithTitle:@"密码不正确" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        self.passWordText.text = nil;
                    }];
                    [alertContrller addAction:sureAlertAction];
                    [self presentViewController:alertContrller animated:YES completion:nil];
                }
            }
        }];
    }else{//添加用户密码为空
        UIAlertController *alertContrller = [UIAlertController alertControllerWithTitle:@"用户名密码不能为空" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertContrller addAction:sureAlertAction];
        [self presentViewController:alertContrller animated:YES completion:nil];
    }
    
    
}




- (void)add{
    
    self.titleImageV = [[UIImageView alloc]init];
    
    self.titleImageV.frame = CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height*0.3);
    
    self.titleImageV.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:self.titleImageV];
    
    
    self.phoneNumberText = [[UITextField alloc]init];
    
    self.phoneNumberText.frame = CGRectMake(40, CGRectGetMaxY(self.titleImageV.frame), CGRectGetWidth(self.titleImageV.frame)-40, 40);
    
    self.phoneNumberText.placeholder = @"用户名";
    
    [self.view addSubview:self.phoneNumberText];
    
    self.passWordText = [[UITextField alloc]init];
    
    self.passWordText.frame = CGRectMake(40, CGRectGetMaxY(self.phoneNumberText.frame), CGRectGetWidth(self.phoneNumberText.frame)-40, 40);
    
    self.passWordText.placeholder = @"密码";
    
    self.passWordText.secureTextEntry = YES;
    
    [self.view addSubview:self.passWordText];
    
    
    self.forgetBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    self.forgetBtn.frame = CGRectMake(20, CGRectGetMaxY(self.passWordText.frame)+30, 150, 30);
    
    [self.forgetBtn setTitle:@"忘记密码" forState:(UIControlStateNormal)];
    
    [self.view addSubview:self.forgetBtn];
    
    
    self.registBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    self.registBtn.frame = CGRectMake(self.view.frame.size.width- 200, CGRectGetMinY(self.forgetBtn.frame), 150, 30);
    
    [self.registBtn setTitle:@"注册新会员" forState:(UIControlStateNormal)];
    
    [self.view addSubview:self.registBtn];
    
    self.QQBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    [self.QQBtn setTitle:@"QQ" forState:(UIControlStateNormal)];
    
    self.QQBtn.frame = CGRectMake(self.view.frame.size.width*0.5-60, self.view.frame.size.height*0.8, 50, 50);
    
    [self.view addSubview:self.QQBtn];
    
    self.sinaBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    self.sinaBtn.frame = CGRectMake(CGRectGetMaxX(self.QQBtn.frame)+30, CGRectGetMinY(self.QQBtn.frame), 50, 50);
    
    [self.sinaBtn setTitle:@"分享" forState:(UIControlStateNormal)];
    
    [self.view addSubview:self.sinaBtn];
    
    
}

- (void)registBtnAction{
    
    RegistViewController *regist = [[RegistViewController alloc]init];
    
    [self.navigationController pushViewController:regist animated:YES];
    
}

- (void)forgetBtnAction{
    
    ForgetViewController *forget = [[ForgetViewController alloc]init];
    
    [self.navigationController pushViewController:forget animated:YES];
}

- (void)QQBtnAction{
    
    NSLog(@"222222");
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            MyViewController *my = [[MyViewController alloc]init];
            
            
            HELP.userName = snsAccount.userName;
            
            HELP.iconURL = snsAccount.iconURL;
            
            HELP.loginToView = YES;
            
            [self.navigationController pushViewController:my animated:YES];
        }
    });
    
    
    
    
    
}

//分享按钮
- (void)sinaBtnAction{
    
    
    //    [UMSocialSnsService presentSnsIconSheetView:self appKey:nil shareText:@"欢迎使用旅游助手" shareImage:nil shareToSnsNames:@[UMShareToSina,UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline] delegate:self];
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"56e1345567e58e9de0000782" shareText:@"旅游助手" shareImage:nil shareToSnsNames:@[UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToQQ,UMShareToWechatSession,UMShareToWechatTimeline]delegate:self];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(BOOL)isDirectShareInIconActionSheet
{
    return NO;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
